using System;
using System.Drawing;
using System.Windows.Forms;
using Diagram.Model;
using Diagram.View;
using PetriDiagram.Model;

namespace PetriDiagram.Tools
{
    [Serializable]
    public class RemoveTokenTool : TokenTool
    {
        public RemoveTokenTool( EntityManager entityManager, Diagram.View.Diagram diagram )
            : base( entityManager, diagram )
        {
            Name = "Remove token";
            cursor = Cursors.Cross;
        }

        public override void OnLMBDown( Point location )
        {
            Shape shape = Diagram.FindShape( location );

            if( shape == null )
                return;

            if( shape.Entity is Place )
            {
                Place p = shape.Entity as Place;
                p.Tokens--;

                if( p.Tokens < 0 )
                    p.Tokens = 0;
            }
            else if( shape.Entity is PetriNetRelation )
            {
                PetriNetRelation r = shape.Entity as PetriNetRelation;
                r.Weight--;

                if( r.Weight < 1 )
                    r.Weight = 0;
            }

            base.OnLMBDown( location );
        }
    }
}
